package com.ttb.smshelper;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.List;

import com.ttb.privatebox.CustomizedListView;
import com.ttb.smshelper.MainActivity;
import com.ttb.smshelper.chanmessage;
import com.ttb.smshelper.lichmessage;
import com.ttb.smshelper.xoamessage;
import com.ttb.schedule.ScheduleHandler;
import com.ttb.smshelper.R;
import com.ttb.smshelper.md5.Md5Service;
import com.ttb.blocksms.BlockService;
import com.ttb.database.Blocker;
import com.ttb.database.BlockerDAO;
import com.ttb.database.Contact;
import com.ttb.database.ContactDAO;
import com.ttb.database.DBHandler;
import com.ttb.database.User;
import com.ttb.database.UserDAO;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.sqlite.SQLiteDatabase;
import android.telephony.SmsManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

public class MainActivity extends Activity {
	public static final String TYPE_NO_PHONE = "NUMBER_PHONE";
	private BlockService schedule;
	private Context context;
	private ImageButton private_box;
	private ImageButton chan;
	private ImageButton lich;
	final private static int DIALOG_LOGIN = 1;
	final private static int DIALOG_PASS = 2;
	private EditText userName;
	private EditText password;
	private UserDAO userDao;
	private Md5Service md5Service = new Md5Service();
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		context = this.getApplicationContext();
		DBHandler dbhandle = new DBHandler(context);
		userDao = new UserDAO(context);
		// checkSmsReciver();
		 
		Intent intent = new Intent();
		intent.setAction("com.ttb.smshelper.SCHEDULE_SMS");
		sendBroadcast(intent);

		// schedule = new BlockService();
		// if (schedule != null) {
		// schedule.SetAlarm(this.getApplicationContext(),"123");
		// } else {
		// Toast.makeText(this.getApplicationContext(), "Alarm is null",
		// Toast.LENGTH_SHORT).show();
		// }
		private_box = (ImageButton) findViewById(R.id.btnprivate);
		chan = (ImageButton) findViewById(R.id.btnchan);
		lich = (ImageButton) findViewById(R.id.btnlich);
		private_box.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				if (checkRegistered()) {
					showDialog(DIALOG_PASS);
				} else {
					showDialog(DIALOG_LOGIN);
				}

			}
		});
		chan.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(MainActivity.this, chanmessage.class);
				startActivity(intent);
			}
		});
		lich.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(MainActivity.this, lichmessage.class);
				startActivity(intent);
			}
		});
	}

	@Override
	protected void onPause() {
		// if (schedule != null) {
		// schedule.CancelAlarm(context);
		// }
		super.onPause();

	}

	@Override
	protected void onDestroy() {
		// schedule.CancelAlarm(context);
		super.onDestroy();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	// ---sends an SMS message to another device---
	public void checkSmsReciver() {
		String SENT = "SMS_SENT";
		String DELIVERED = "SMS_DELIVERED";
		// ---when the SMS has been sent---
		registerReceiver(new BroadcastReceiver() {
			@Override
			public void onReceive(Context arg0, Intent arg1) {
				switch (getResultCode()) {
				case Activity.RESULT_OK:
					Toast.makeText(getBaseContext(), "SMS sent",
							Toast.LENGTH_SHORT).show();
					break;
				case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
					Toast.makeText(getBaseContext(), "Generic failure",
							Toast.LENGTH_SHORT).show();
					break;
				case SmsManager.RESULT_ERROR_NO_SERVICE:
					Toast.makeText(getBaseContext(), "No service",
							Toast.LENGTH_SHORT).show();
					break;
				case SmsManager.RESULT_ERROR_NULL_PDU:
					Toast.makeText(getBaseContext(), "Null PDU",
							Toast.LENGTH_SHORT).show();
					break;
				case SmsManager.RESULT_ERROR_RADIO_OFF:
					Toast.makeText(getBaseContext(), "Radio off",
							Toast.LENGTH_SHORT).show();
					break;
				}
			}
		}, new IntentFilter(SENT));

		// ---when the SMS has been delivered---
		registerReceiver(new BroadcastReceiver() {
			@Override
			public void onReceive(Context arg0, Intent arg1) {
				switch (getResultCode()) {
				case Activity.RESULT_OK:
					Toast.makeText(getBaseContext(), "SMS delivered",
							Toast.LENGTH_SHORT).show();
					break;
				case Activity.RESULT_CANCELED:
					Toast.makeText(getBaseContext(), "SMS not delivered",
							Toast.LENGTH_SHORT).show();
					break;
				}
			}
		}, new IntentFilter(DELIVERED));
	}

	public void sendSMS(String phoneNumber, String message) {
		String SENT = "SMS_SENT";
		String DELIVERED = "SMS_DELIVERED";

		PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(
				SENT), 0);

		PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,
				new Intent(DELIVERED), 0);

		// ---when the SMS has been sent---
		registerReceiver(new BroadcastReceiver() {
			@Override
			public void onReceive(Context arg0, Intent arg1) {
				switch (getResultCode()) {
				case Activity.RESULT_OK:
					Toast.makeText(getBaseContext(), "SMS sent",
							Toast.LENGTH_SHORT).show();
					break;
				case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
					Toast.makeText(getBaseContext(), "Generic failure",
							Toast.LENGTH_SHORT).show();
					break;
				case SmsManager.RESULT_ERROR_NO_SERVICE:
					Toast.makeText(getBaseContext(), "No service",
							Toast.LENGTH_SHORT).show();
					break;
				case SmsManager.RESULT_ERROR_NULL_PDU:
					Toast.makeText(getBaseContext(), "Null PDU",
							Toast.LENGTH_SHORT).show();
					break;
				case SmsManager.RESULT_ERROR_RADIO_OFF:
					Toast.makeText(getBaseContext(), "Radio off",
							Toast.LENGTH_SHORT).show();
					break;
				}
			}
		}, new IntentFilter(SENT));

		// ---when the SMS has been delivered---
		registerReceiver(new BroadcastReceiver() {
			@Override
			public void onReceive(Context arg0, Intent arg1) {
				switch (getResultCode()) {
				case Activity.RESULT_OK:
					Toast.makeText(getBaseContext(), "SMS delivered",
							Toast.LENGTH_SHORT).show();
					break;
				case Activity.RESULT_CANCELED:
					Toast.makeText(getBaseContext(), "SMS not delivered",
							Toast.LENGTH_SHORT).show();
					break;
				}
			}
		}, new IntentFilter(DELIVERED));
		SmsManager sms = SmsManager.getDefault();
		sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
	}

	@Override
	protected Dialog onCreateDialog(int id) {

		AlertDialog dialogDetails = null;

		switch (id) {
		case DIALOG_LOGIN:
			LayoutInflater inflater = LayoutInflater.from(this);
			View dialogview = inflater.inflate(R.layout.dialog_layout, null);
			AlertDialog.Builder dialogbuilder = new AlertDialog.Builder(
					MainActivity.this);
			dialogbuilder.setTitle("Set Password");
			dialogbuilder.setView(dialogview);
			dialogDetails = dialogbuilder.create();
			break;
		case DIALOG_PASS:
			LayoutInflater inflater1 = LayoutInflater.from(this);
			View dialogview1 = inflater1.inflate(R.layout.dialog_pass, null);
			AlertDialog.Builder dialogbuilder1 = new AlertDialog.Builder(
					MainActivity.this);
			dialogbuilder1.setTitle("Enter Password");
			dialogbuilder1.setView(dialogview1);
			dialogDetails = dialogbuilder1.create();
			break;
		}

		return dialogDetails;
	}

	@Override
	protected void onPrepareDialog(int id, Dialog dialog) {

		switch (id) {
		case DIALOG_LOGIN:
			final AlertDialog alertDialog = (AlertDialog) dialog;
			Button loginbutton = (Button) alertDialog
					.findViewById(R.id.btn_login);
			Button cancelbutton = (Button) alertDialog
					.findViewById(R.id.btn_cancel);
			userName = (EditText) alertDialog.findViewById(R.id.editpass);
			password = (EditText) alertDialog.findViewById(R.id.editconfirm);

			loginbutton.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {
					alertDialog.dismiss();
					if (userName.getText().toString()
							.equals(password.getText().toString())) {
						addUser(userName.getText().toString());
						Intent intent = new Intent(MainActivity.this,
								CustomizedListView.class);
						startActivity(intent);
					}
				}
			});
			cancelbutton.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					alertDialog.dismiss();
				}
			});
			break;
		case DIALOG_PASS:
			final AlertDialog alertDialog1 = (AlertDialog) dialog;
			Button loginbutton1 = (Button) alertDialog1
					.findViewById(R.id.btn_login1);
			Button cancelbutton1 = (Button) alertDialog1
					.findViewById(R.id.btn_cancel1);
			final EditText password = (EditText) alertDialog1
					.findViewById(R.id.editpass1);

			loginbutton1.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {
					if (comparePass(password.getText().toString())) {
						Intent intent = new Intent(MainActivity.this,
								CustomizedListView.class);
						startActivity(intent);
					}
					alertDialog1.dismiss();
				}
			});

			cancelbutton1.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {
					alertDialog1.dismiss();
				}
			});

		}
	}

	public Boolean checkRegistered() {
		int count = userDao.getUserCount();
		if (count > 0) {
			return true;
		}
		return false;
	}

	private User addUser(String password) {
		
		User user = new User();
		
		user.setPassword(md5Service.genarateMD5(password));
		userDao.addUser(user);
		return user;
	}

	private Boolean comparePass(String input) {
		int count = userDao.getUserCount();
			if (count == 1) {
				User user = userDao.getUser(1);
				String password = user.getPassword();
				String inputPass = md5Service.genarateMD5(input);
				if (password.equals(inputPass)) {
					return true;
				}
			}
		return false;
	}
}
